<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="解调时间间隔" prop="demodulationInterval">
      <el-input v-model="dataForm.demodulationInterval" placeholder="解调时间间隔"></el-input>
    </el-form-item>
    <el-form-item label="光衰减器（v）" prop="attenuation">
      <el-input v-model="dataForm.attenuation" placeholder="光衰减器（v）"></el-input>
    </el-form-item>
    <el-form-item label="解调方法 0-Buneman 1-质心法 2-高斯拟合" prop="demodulationMethod">
      <el-input v-model="dataForm.demodulationMethod" placeholder="解调方法 0-Buneman 1-质心法 2-高斯拟合"></el-input>
    </el-form-item>
    <el-form-item label="位移变化速率阈值" prop="changeRateThreshold">
      <el-input v-model="dataForm.changeRateThreshold" placeholder="位移变化速率阈值"></el-input>
    </el-form-item>
    <el-form-item label="最大位移变化阈值" prop="changeMaxThreshold">
      <el-input v-model="dataForm.changeMaxThreshold" placeholder="最大位移变化阈值"></el-input>
    </el-form-item>
    <el-form-item label="光开关数量" prop="switchNum">
      <el-input v-model="dataForm.switchNum" placeholder="光开关数量"></el-input>
    </el-form-item>
    <el-form-item label="每组光开关FBG数量" prop="switchFbgNum">
      <el-input v-model="dataForm.switchFbgNum" placeholder="每组光开关FBG数量"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="gmtCreate">
      <el-input v-model="dataForm.gmtCreate" placeholder="创建时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          demodulationInterval: '',
          attenuation: '',
          demodulationMethod: '',
          changeRateThreshold: '',
          changeMaxThreshold: '',
          switchNum: '',
          switchFbgNum: '',
          gmtCreate: ''
        },
        dataRule: {
          demodulationInterval: [
            { required: true, message: '解调时间间隔不能为空', trigger: 'blur' }
          ],
          attenuation: [
            { required: true, message: '光衰减器（v）不能为空', trigger: 'blur' }
          ],
          demodulationMethod: [
            { required: true, message: '解调方法 0-Buneman 1-质心法 2-高斯拟合不能为空', trigger: 'blur' }
          ],
          changeRateThreshold: [
            { required: true, message: '位移变化速率阈值不能为空', trigger: 'blur' }
          ],
          changeMaxThreshold: [
            { required: true, message: '最大位移变化阈值不能为空', trigger: 'blur' }
          ],
          switchNum: [
            { required: true, message: '光开关数量不能为空', trigger: 'blur' }
          ],
          switchFbgNum: [
            { required: true, message: '每组光开关FBG数量不能为空', trigger: 'blur' }
          ],
          gmtCreate: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/config/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.demodulationInterval = data.config.demodulationInterval
                this.dataForm.attenuation = data.config.attenuation
                this.dataForm.demodulationMethod = data.config.demodulationMethod
                this.dataForm.changeRateThreshold = data.config.changeRateThreshold
                this.dataForm.changeMaxThreshold = data.config.changeMaxThreshold
                this.dataForm.switchNum = data.config.switchNum
                this.dataForm.switchFbgNum = data.config.switchFbgNum
                this.dataForm.gmtCreate = data.config.gmtCreate
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/config/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'demodulationInterval': this.dataForm.demodulationInterval,
                'attenuation': this.dataForm.attenuation,
                'demodulationMethod': this.dataForm.demodulationMethod,
                'changeRateThreshold': this.dataForm.changeRateThreshold,
                'changeMaxThreshold': this.dataForm.changeMaxThreshold,
                'switchNum': this.dataForm.switchNum,
                'switchFbgNum': this.dataForm.switchFbgNum,
                'gmtCreate': this.dataForm.gmtCreate
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
